package com.example.urmsadmin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.urmsadmin.Util.ValidatorUtil;
import com.example.urmsadmin.controller.SysUserController;
import com.example.urmsadmin.entity.SysUser;
import com.example.urmsadmin.mapper.UserMapper;
import com.example.urmsadmin.service.SysUserService;
import com.example.urmsadmin.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 08:41:17
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<UserMapper, SysUser>  implements SysUserService {

    @Autowired
    SysUserController sysUserController;

    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importUser(List<SysUser> userList, Boolean isUpdateSupport)
    {
        if (userList==null || userList.size() == 0)
        {
            return  "导入用户数据不能为空！";
        }
        int successNum = 0;
        int failureNum = 0;
        int lineNum  = 1;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysUser user : userList)
        {
            lineNum++;
            try
            {
                // 验证是否存在这个用户
                CommonResult checkUserName = sysUserController.checkUserName(user.getUserName());
                if (checkUserName.getCode() == 200)
                {
                    boolean validator = true;
                    if(!ValidatorUtil.isUserName(user.getUserName())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、用户名 `" + user.getUserName() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isTrueName(user.getTrueName())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、姓名 `" + user.getTrueName() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isPassword(user.getPassword())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、密码 `" + user.getPassword() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isPhonenumber(user.getPhonenumber())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、手机号码 `" + user.getPhonenumber() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isRemark(user.getRemark())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、邮箱 `" + user.getEmail() + "` 格式错误");
                        validator = false;
                    }
                    if(validator){
                        user.setSex("男".equals(user.getSex())?"0":"女".equals(user.getSex())?"1":"2");
                        user.setStatus("有效".equals(user.getStatus())?"0":"1");
                        CommonResult addUser = sysUserController.addUser(user);
                        if(addUser.getCode() ==200){
                            successNum++;
                            successMsg.append("<br/>第" + lineNum + "行:" + successNum + "、账号 `" + user.getUserName() + "` 导入成功");
                        }else{
                            failureNum++;
                            failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、账号 `" + user.getUserName() + "` 导入失败");
                        }
                    }
                }
                else if (isUpdateSupport)
                {
                    boolean validator = true;
                    if(!ValidatorUtil.isTrueName(user.getTrueName())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、姓名 `" + user.getTrueName() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isPassword(user.getPassword())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、密码 `" + user.getPassword() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isPhonenumber(user.getPhonenumber())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、手机号码 `" + user.getPhonenumber() + "` 格式错误");
                        validator = false;
                    }
                    if(!ValidatorUtil.isRemark(user.getRemark())){
                        failureNum++;
                        failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、邮箱 `" + user.getEmail() + "` 格式错误");
                        validator = false;
                    }
                    if(validator){
                        user.setSex("男".equals(user.getSex())?"0":"女".equals(user.getSex())?"1":"2");
                        user.setStatus("有效".equals(user.getStatus())?"0":"1");
                        CommonResult addUser = sysUserController.updateUser(user);
                        if(addUser.getCode() ==200){
                            successNum++;
                            successMsg.append("<br/>第" + lineNum + "行:" + successNum + "、账号 `" + user.getUserName() + "` 更新成功");
                        }else{
                            failureNum++;
                            failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、账号 `" + user.getUserName() + "` 更新失败");
                        }
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>第" + lineNum + "行:" + failureNum + "、账号 `" + user.getUserName() + "` 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>第" + lineNum + "行:" + failureNum + "、账号 `" + user.getUserName() + "` 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        String message = successNum + "个用户导入成功：" + successMsg + "<br/>" + "导入失败信息("+ failureNum + "条)：" + failureMsg;
        return message;
    }
}
